using System.Linq.Expressions;
using System.Reflection;
using Gateway.Common.Entity;
using Gateway.Common.Enums;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Gateway.Domain.EFCore.QueryFilters;

public static class DeletedQueryFilter
{
    public static void AddDeletedQueryFilter(this IMutableEntityType entityType)
    {
        var methodToCall = typeof(DeletedQueryFilter)
            .GetMethod(nameof(GetDeletedFilter),
                BindingFlags.NonPublic | BindingFlags.Static)
            ?.MakeGenericMethod(entityType.ClrType);
        var filter = methodToCall?.Invoke(null, new object[] { });
        entityType.SetQueryFilter((LambdaExpression)filter!);
    }

    private static LambdaExpression GetDeletedFilter<TEntity>()
        where TEntity : EntityWithOperatorStatusBase
    {
        Expression<Func<TEntity, bool>> filter = x => BooleanStatusEnum.No == x.Deleted;
        return filter;
    }
}